//
//  PreviewController.swift
//  Gallery-iOS
//
//  Created by qifanxin on 2021/9/23.
//  Copyright © 2021 Hyper Interaktiv AS. All rights reserved.
//

import Foundation
import UIKit

class PreviewController : UIViewController {
    
    public weak var delegate: GalleryDelegate?
    
    lazy var previewView = makePreview()
    
    let cart: Cart
    
    public required init(cart: Cart) {
      self.cart = cart
      super.init(nibName: nil, bundle: nil)
    }
    
    override func viewDidAppear(_ animated: Bool){
        super.viewDidAppear(animated)
        delegate?.removeView(controller: self)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        previewView.willRemoveSubview(previewView.bottomContainer)
    }
    
    override func viewDidLoad() {
      super.viewDidLoad()
      view.backgroundColor = UIColor.black
      setup()
    }
    
    public required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
    }
    
    func setup() {
        view.addSubview(previewView)
        previewView.g_pinEdges()
        previewView.retryButton.addTarget(self, action: #selector(retryButtonTouched), for: .touchUpInside)
        previewView.confirmButton.addTarget(self, action: #selector(confirmButtonTouched), for: .touchUpInside)
    }
    
    func makePreview() -> PreviewView {
        let view = PreviewView(cart: self.cart)
        return view
    }
    
    @objc func retryButtonTouched(button: UIButton) {
        EventHub.shared.doneWithTakePhoto?()
    }
    
    @objc func confirmButtonTouched(button: UIButton) {
        EventHub.shared.doneWithImages?()
    }
}

